package com.ysactive.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ysactive.config.BaseResponse;
import com.ysactive.config.BusinessException;
import com.ysactive.config.ErrorCodeEnume;
import com.ysactive.dto.SendEmailDto;
import com.ysactive.dto.UserLoginDto;
import com.ysactive.dto.UserRegisterDto;
import com.ysactive.entity.UserEntity;
import com.ysactive.service.FileService;
import com.ysactive.service.UserService;
import com.ysactive.util.YsUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gcj
 * @since 2023-09-02
 */
@AllArgsConstructor
@Controller
@RequestMapping("/user")
public class UserController {

    private static final Map<String, String> EMAIL_CODE = new HashMap<>();

    private final UserService userService;
    private final FileService fileService;

    /***
     * login
     * @author gaochaojie
     * @param dto
     * @return com.ysactive.config.BaseResponse<com.ysactive.entity.UserEntity>
     **/
    @PostMapping("/login")
    @ResponseBody
    public BaseResponse<UserEntity> login(@RequestBody UserLoginDto dto) {
        if(!YsUtil.checkType(dto.getType())){
            throw new BusinessException(ErrorCodeEnume.TYPE_ERROR);
        }
        LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<UserEntity>();
        queryWrapper.eq(UserEntity::getUserEmail, dto.getUserEmail());
        queryWrapper.eq(UserEntity::getUserPwd, dto.getUserPwd());
        UserEntity one = userService.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(one)) {
            if ("01".equals(dto.getType())) {
                throw new BusinessException(ErrorCodeEnume.LOGIN_ERROR_01);
            }
            if ("02".equals(dto.getType())) {
                throw new BusinessException(ErrorCodeEnume.LOGIN_ERROR_02);
            }
            if ("03".equals(dto.getType())) {
                throw new BusinessException(ErrorCodeEnume.LOGIN_ERROR_03);
            }
            if ("04".equals(dto.getType())) {
                throw new BusinessException(ErrorCodeEnume.LOGIN_ERROR_04);
            }
        }
        return BaseResponse.success(one);
    }

    @PostMapping("/sendEmail")
    @ResponseBody
    public BaseResponse<Object> sendEmail(@RequestBody SendEmailDto dto) {
        if(!YsUtil.checkType(dto.getType())){
            throw new BusinessException(ErrorCodeEnume.TYPE_ERROR);
        }
        String code = YsUtil.getyzm();
        if(ObjectUtil.isNotEmpty(EMAIL_CODE.get(dto.getUserEmail()))){

            if ("01".equals(dto.getType())) {
                throw new BusinessException(ErrorCodeEnume.EMAIL_ERROR1_01);
            }
            if ("02".equals(dto.getType())) {
                throw new BusinessException(ErrorCodeEnume.EMAIL_ERROR2_02);
            }
            if ("03".equals(dto.getType())) {
                throw new BusinessException(ErrorCodeEnume.EMAIL_ERROR3_03);
            }
            if ("04".equals(dto.getType())) {
                throw new BusinessException(ErrorCodeEnume.EMAIL_ERROR4_04);
            }
        }
        //发送邮件
        EMAIL_CODE.put(dto.getUserEmail(), code);
        fileService.sendRegCode(dto.getUserEmail(), code);
        return BaseResponse.success(code);
    }

    @PostMapping("/register")
    @ResponseBody
    public BaseResponse<Object> register(@RequestBody UserRegisterDto dto) {
        if(!YsUtil.checkType(dto.getType())){
            throw new BusinessException(ErrorCodeEnume.TYPE_ERROR);
        }
        if (ObjectUtil.isEmpty(EMAIL_CODE.get(dto.getUserEmail()))) {
            if ("01".equals(dto.getType())) {
                throw new BusinessException(ErrorCodeEnume.EMAIL_ERROR5_01);
            }
            if ("02".equals(dto.getType())) {
                throw new BusinessException(ErrorCodeEnume.EMAIL_ERROR6_02);
            }
            if ("03".equals(dto.getType())) {
                throw new BusinessException(ErrorCodeEnume.EMAIL_ERROR7_03);
            }
            if ("04".equals(dto.getType())) {
                throw new BusinessException(ErrorCodeEnume.EMAIL_ERROR8_04);
            }
        }
        if (!EMAIL_CODE.get(dto.getUserEmail()).equals(dto.getEmailCode())) {
            if ("01".equals(dto.getType())) {
                throw new BusinessException(ErrorCodeEnume.EMAIL_ERROR5_01);
            }
            if ("02".equals(dto.getType())) {
                throw new BusinessException(ErrorCodeEnume.EMAIL_ERROR6_02);
            }
            if ("03".equals(dto.getType())) {
                throw new BusinessException(ErrorCodeEnume.EMAIL_ERROR7_03);
            }
            if ("04".equals(dto.getType())) {
                throw new BusinessException(ErrorCodeEnume.EMAIL_ERROR8_04);
            }
        }
        UserEntity entity = BeanUtil.copyProperties(dto, UserEntity.class);
        entity.setCreateTime(LocalDateTime.now());
        userService.save(entity);
        EMAIL_CODE.remove(entity.getUserEmail());
        return BaseResponse.success(null);
    }


}

